package com.tdam2011.grupo05.utils.preference;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class PreferencesHelper {

	public static final String KEY_USERNAME = "username";
	public static final String KEY_PASSWORD = "password";
	public static final String KEY_SERVER_URL = "server_url";
	public static final String KEY_MESSAGE_NUMBER = "message_number";
	public static final String KEY_SYNC = "sync_switch";
	public static final String KEY_SYNC_TIME = "sync_time";
	public static final String KEY_LAST_SYNC = "last_sync";
	public static final String KEY_INITIAL_CONFIG = "initial_config";
	public static final String KEY_ACCOUNT_CONFIG = "account_config";
	public static final String KEY_SYNC_MESSAGE_NOT_SENDED = "sync_message_not_sended";
	public static final String DEFAULT_VALUE = "";

	public static final String DEFAULT_STORAGE_NUMBER = "500";
	public static final String DEFAULT_SYNC_TIME = "1";

	private final SharedPreferences mSharedPreferences;

	public PreferencesHelper(final Context context) {
		mSharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
	}

	public String getServerUrl() {
		String uriString = mSharedPreferences.getString(KEY_SERVER_URL, "");
		return uriString;
	}

	public String getUsername() {
		String username = mSharedPreferences.getString(KEY_USERNAME,
				DEFAULT_VALUE);
		return username;
	}

	public void saveUsername(final String username) {
		SharedPreferences.Editor mEditor = mSharedPreferences.edit();
		mEditor.putString(KEY_USERNAME, username);
		mEditor.commit();
	}

	public String getPassword() {
		String pass = mSharedPreferences.getString(KEY_PASSWORD, DEFAULT_VALUE);
		return pass;
	}

	public void savePassword(final String password) {
		SharedPreferences.Editor mEditor = mSharedPreferences.edit();
		mEditor.putString(KEY_PASSWORD, password);
		mEditor.commit();
	}

	public int getMessageMaxStorageNumber() {
		String number = mSharedPreferences.getString(KEY_MESSAGE_NUMBER,
				DEFAULT_STORAGE_NUMBER);
		return Integer.parseInt(number);
	}

	public void saveMessageMaxStorageNumber(final int value) {
		SharedPreferences.Editor mEditor = mSharedPreferences.edit();
		mEditor.putString(KEY_MESSAGE_NUMBER, Integer.toString(value));
		mEditor.commit();
	}

	public int getSyncTime() {
		String number = mSharedPreferences.getString(KEY_SYNC_TIME, "0");
		return Integer.parseInt(number);
	}

	public void saveSyncTime(final int value) {
		SharedPreferences.Editor mEditor = mSharedPreferences.edit();
		mEditor.putInt(KEY_SYNC_TIME, value);
		mEditor.commit();
	}

	public int getReSyncTime() {
		String number = mSharedPreferences.getString(
				KEY_SYNC_MESSAGE_NOT_SENDED, "60");
		return Integer.parseInt(number);
	}

	public void saveReSyncTime(final int value) {
		SharedPreferences.Editor mEditor = mSharedPreferences.edit();
		mEditor.putInt(KEY_SYNC_MESSAGE_NOT_SENDED, value);
		mEditor.commit();
	}

	public boolean getSyncState() {
		boolean state = mSharedPreferences.getBoolean(KEY_SYNC, true);
		return state;
	}

	public void setSyncState(final boolean status) {
		SharedPreferences.Editor mEditor = mSharedPreferences.edit();
		mEditor.putBoolean(KEY_SYNC, status);
		mEditor.commit();
	}

	public long getLastSync() {
		return mSharedPreferences.getLong(KEY_LAST_SYNC, 0);
	}

	public void updateLastSync(final long timestamp) {
		SharedPreferences.Editor mEditor = mSharedPreferences.edit();
		mEditor.putLong(KEY_LAST_SYNC, timestamp);
		mEditor.commit();
	}

	public boolean getInitialConfigState() {
		return mSharedPreferences.getBoolean(KEY_INITIAL_CONFIG, false);
	}

	public void setInitialConfigState(final boolean status) {
		SharedPreferences.Editor mEditor = mSharedPreferences.edit();
		mEditor.putBoolean(KEY_INITIAL_CONFIG, status);
		mEditor.commit();
	}

	public boolean getAccountConfigStatus() {
		return mSharedPreferences.getBoolean(KEY_ACCOUNT_CONFIG, false);
	}

	public void saveAccountConfigStatus(final boolean status) {
		SharedPreferences.Editor mEditor = mSharedPreferences.edit();
		mEditor.putBoolean(KEY_ACCOUNT_CONFIG, status);
		mEditor.commit();
	}

	public void setServerUrl(String server) {
		SharedPreferences.Editor mEditor = mSharedPreferences.edit();
		mEditor.putString(KEY_SERVER_URL, server);
		mEditor.commit();

	}
}
